package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductDao;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductImpl implements ProductDao {
    @Override
    public int getCount(ProductModel productModel) {
        String sql = "select count(*) count from product";
        ResultSet resultSet = JDBC.query(sql);
        try {
            while (resultSet.next()) {
                return resultSet.getInt("count");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<ProductModel> findAll(ProductModel productModel) {
        String sql = "select * from product order by id desc";
        sql += " limit " + (productModel.getPageBean().getPage() - 1) * productModel.getPageBean().getPageSize() + "," + productModel.getPageBean().getPageSize() + ";";
        ResultSet resultSet = JDBC.query(sql);
        List<ProductModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                ProductModel model = new ProductModel();
                model.setId(resultSet.getInt("id"));
                model.setNormalPrice(resultSet.getFloat("normal_price"));
                model.setMarketPrice(resultSet.getFloat("price"));
                model.setName(resultSet.getString("name"));
                model.setImgHref(resultSet.getString("img_href"));
                model.setCreateTime(resultSet.getString("create_time"));
                model.setUpdateTime(resultSet.getDate("update_time"));
                model.setEnable(resultSet.getInt("enable"));
                model.setContent(resultSet.getString("content"));
                model.setIsShow(resultSet.getInt("is_show"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int add(ProductModel productModel) {
        String sql = "";
        return JDBC.update(sql);
    }

    @Override
    public ProductModel findById(int id) {
        String sql = "select * from product where id=" + id;
        ResultSet resultSet = JDBC.query(sql);
        ProductModel model = new ProductModel();
        try {
            while (resultSet.next()) {
                model.setId(resultSet.getInt("id"));
                model.setNormalPrice(resultSet.getFloat("normal_price"));
                model.setMarketPrice(resultSet.getFloat("price"));
                model.setName(resultSet.getString("name"));
                model.setImgHref(resultSet.getString("img_href"));
                model.setCreateTime(resultSet.getString("create_time"));
                model.setUpdateTime(resultSet.getDate("update_time"));
                model.setEnable(resultSet.getInt("enable"));
                model.setContent(resultSet.getString("content"));
                model.setIsShow(resultSet.getInt("is_show"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public int update(ProductModel productModel) {
        String sql = "UPDATE `product` SET " +
                "`name` = '1'," +
                " `normal_price` = '1', " +
                "`price` = '1', " +
                "`content` = '1', " +
                "`is_show` = '1', " +
                "`is_recommend` = '1',";
        if (!"".equals(productModel.getImgHref())) {
            sql += " `img_href` = '" + productModel.getImgHref() + "',";
        }

        sql += " `cuisine_id` = '1'," +
                " `update_time` = now(), " +
                "`remark` = '1'," +
                " `enable` = '1', " +
                "`where_show` = '1' " +
                "WHERE (`id` = '72');\n";
        return JDBC.update(sql);
    }
}
